<?php


namespace app\api\controller\v1;


use app\common\controller\UserBaseController;
use app\api\model\BikeAlertModel;

class BalertController extends UserBaseController
{
    function infoAction() {
        $this->paramlist = array(
            'token' => 'string',
            'id' => 'string',
        );

        $this->check_params();
        $this->check_token();

        $info = (new BikeAlertModel())->info($this->city_id, $this->argv['id']);
        $this->resp['info'] = $info;

        return $this->ret_resp();
    }

    function listAction() {
        $this->paramlist = array(
            'token' => 'string',
            'type' => 'optint',
            'health_state' => 'optint',
            'is_under_volt' => 'optint',
            'order_state' => 'optint',
            'start_time' => 'optstring',
            'end_time' => 'optstring',
            'sk' => 'optstring',
            'pno' => 'int',
            'psize' => 'int',
            'is_solved' => 'optint',
            'duration_interval' => 'optintarray',
        );

        $this->check_params();
        $this->check_token();

        $list = (new BikeAlertModel())->get_list($this->city_id, $this->argv, $this->argv['pos'], $this->argv['limit']);
        $this->resp['list'] = $list;
        if (empty($list)) {
            $this->resp['total'] = 0;
        } else {
            $this->resp['total'] = count($list);
        }

        return $this->ret_resp();
    }

    function addAction() {
        $this->paramlist = array(
            'token' => 'string',
            'bike_id' => 'string',
            'type' => 'int',
            'content' => 'string',
        );

        $this->check_params();
        $this->check_token();

        $bike_info = BikeModel::instance()->get_info_by_id($this->argv['bike_id']);

        if (empty($bike_info)) {
            $this->throw_myexception(E_ERROR, "get bike info error!");
        }

        if ($bike_info['alert_state'] & $this->argv['type']) {
            $this->throw_myexception(E_ERROR, "alert already exists!");
        }

        $info = (new BikeAlertModel())->add($this->city_id, $this->argv['bike_id'], $this->argv['type'], $this->argv['content']);

        $this->resp['info'] = $info;

        return $this->ret_resp();
    }

    function confirmAction() {
        $this->paramlist = array(
            'token' => 'string',
            'id' => 'string',
        );

        $this->check_params();
        $this->check_token();

        $alert_info = (new BikeAlertModel())->info($this->city_id, $this->argv['id']);
        if (empty($alert_info)) {
            $this->throw_myexception(E_ERROR, "get alert info error!");
        }

        if ($alert_info['order_state'] != BIKE_ALERT_STATE_PENDING) {
            $this->throw_myexception(E_ERROR, "alert confirmed!");
        }

        (new BikeAlertModel())->confirm($this->city_id, $this->argv['id'],$alert_info['bike_sn'],$alert_info['type']);

        return $this->ret_resp();
    }

    function handleAction() {
        $this->paramlist = array(
            'token' => 'string',
            'id' => 'string',
            'result' => 'int',
            'handle_note' => 'string',
        );

        $this->check_params();
        $this->check_token();

        (new BikeAlertModel())->handle($this->city_id, $this->argv['id'], $this->argv['result'], $this->argv['handle_note']);

        return $this->ret_resp();
    }
}